﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Autodesk.AutoCAD.Windows;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.ApplicationServices;
using ACADApplication = Autodesk.AutoCAD.ApplicationServices.Application;
using ACADDatabase = Autodesk.AutoCAD.DatabaseServices.Database;
using ACADDocument = Autodesk.AutoCAD.ApplicationServices.Document;

namespace YNCNumberSys
{
    public class ZJGetDBDictionary
    {
        #region 属性
        /// <summary>
        /// 如果数据字典不存在,创建数据字典.
        /// </summary>
        bool _b_NewDBDictionary;
        /// <summary>
        /// 扩展字典名称.
        /// </summary>
        string _str_DBDictionaryName;
        /// <summary>
        /// 扩展字典,返回值.
        /// </summary>
        DBDictionary _dict_DBDictionary;
        /// <summary>
        /// 事务.
        /// </summary>
        Transaction _trs_Transaction;
        /// <summary>
        /// 图纸文档WorkingDatabase,为空表示当前的图纸空间.
        /// </summary>
        private ACADDatabase _wdb_DataBase;
        /// <summary>
        /// 该值如果为True,则(1)_trs_Transaction,_wdb_DataBase均为当前图纸空间;(2)事务提交在该类内完成.
        /// </summary>
        private bool _b_TransactionIsUsed;

        #endregion

        public ZJGetDBDictionary(ACADDatabase wdb_DataBase,
            Transaction trs_Transaction, bool b_NewDBDictionary, string str_DBDictionaryName)
        {
            _wdb_DataBase = wdb_DataBase;
            _trs_Transaction = trs_Transaction;
            _b_NewDBDictionary = b_NewDBDictionary;
            _str_DBDictionaryName = str_DBDictionaryName;
            _dict_DBDictionary = null;
            _b_TransactionIsUsed = true;
        }
        public ZJGetDBDictionary(bool b_NewDBDictionary, string str_DBDictionaryName)
        {
            _wdb_DataBase = HostApplicationServices.WorkingDatabase; 
            _trs_Transaction = null;
            _b_NewDBDictionary = b_NewDBDictionary;
            _str_DBDictionaryName = str_DBDictionaryName;
            _dict_DBDictionary = null;
            _b_TransactionIsUsed = false;
        }
        private void DoGetDBDictionary()
        {
            ObjectId id_系统命名字典 = HostApplicationServices.WorkingDatabase.NamedObjectsDictionaryId;
            DBDictionary dct_命名字典 = (DBDictionary)_trs_Transaction.GetObject(id_系统命名字典, OpenMode.ForRead, false);
            if (dct_命名字典.Contains(_str_DBDictionaryName))
            {
                _dict_DBDictionary =
                    (DBDictionary)_trs_Transaction.GetObject(dct_命名字典.GetAt(_str_DBDictionaryName), OpenMode.ForRead, false);
            }
            else
            {
                if (_b_NewDBDictionary)
                {
                    dct_命名字典.UpgradeOpen();
                    _dict_DBDictionary = new DBDictionary();
                    dct_命名字典.SetAt(_str_DBDictionaryName, _dict_DBDictionary);
                    _trs_Transaction.AddNewlyCreatedDBObject(_dict_DBDictionary, true);
                    _dict_DBDictionary.TreatElementsAsHard = true;
                }
            }
        }
        /// <summary>
        /// 执行操作.
        /// </summary>
        /// <returns>返回扩展字典</returns>
        public DBDictionary Do()
        {
            _dict_DBDictionary = null;
            if (!_b_TransactionIsUsed)
            {
                using (_trs_Transaction = _wdb_DataBase.TransactionManager.StartTransaction())
                {
                    DoGetDBDictionary();
                    _trs_Transaction.Commit();
                    _trs_Transaction.Dispose();
                }
            }
            else
            {
                DoGetDBDictionary();
            }
            return _dict_DBDictionary;
        }
    }
}
